<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

define("DIVB_M_PERPAGE",50);

class Common_Model extends CI_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();//$this->load->database('default', TRUE);
	}
	
	public function test()
	{
		$query = $this->db->query('SELECT * FROM t_add');
		$rst = $query->result_array();
		print_r($rst);
	}
	
	public function get_list($table,$where=NULL,$page=1,$perPage=DIVB_M_PERPAGE,$order=NULL)
	{
		$page    = intval($page);
		$num = intval($perPage);
		if ($page < 1) $page = 1;
		$offest    = ($page - 1) * $num;

		$db = $this->db;
        $db->select('*');
        if($where){
        	$db->where($where);
        }
        if($order)
        {
        	$db->order_by($order);
        }
        $db->limit($perPage,$offest);
        $query = $db->get($table);
        if ( $query->num_rows() > 0 ) 
        {
            $array = $query->result_array();
            return $array;
        }
        else 
        {
            return null;
        }
	}
	
	public function get_count($table,$where=array()) {
		$db = $this->db;
        $db->select('count(*)');
	 	if($where){
        	$db->where($where);
        }
	 	$count = $db->count_all_results($table);
        return $count;
	}
	
	public function get_item($table,$where) {
		$db = $this->db;
        $db->select('*');
	 	if($where){
        	$db->where($where);
        }
        $db->limit(1);
		$query = $db->get($table);
        if ( $query->num_rows() > 0 ) 
        {
            $array = $query->result_array();
            return $array[0];
        }
        else 
        {
            return null;
        }
	}
	
	public function update($table,$where,$data) {
		$this->db->where($where);
		return $this->db->update($table, $data); 
	}
	
	public function add($table,$data) {
		return $this->db->insert($table, $data); 
	}
	
	public function delete($table,$where) {
		return $this->db->delete($table,$where); 
	}
	
	public function query_list($sql,$page=1,$perPage=DIVB_M_PERPAGE){
		$page    = intval($page);
		$num = intval($perPage);
		if ($page < 1) $page = 1;
		$offest    = ($page - 1) * $num;
		$limitStr = " limit $offest,$perPage";
		$sql_select = "$sql $limitStr";
		
		$query = $this->db->query($sql_select);
		return $query->result_array();
	}
	
	public function query($sql){
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */